{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.11"
    },
    "colab": {
      "name": "Writing_a_popular_novel.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "AA2d5nbR9FIf",
        "outputId": "33d9f216-af04-46fc-a4ee-0dd5badee4bb",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "%tensorflow_version 1.x\n",
        "!pip install -q gpt-2-simple"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "TensorFlow 1.x selected.\n",
            "  Building wheel for gpt-2-simple (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KHGvrAIk9FIj"
      },
      "source": [
        "# Finetuning the gpt2 model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1F_9BD6K9FIj",
        "outputId": "cd4d2ee4-0c25-4d93-b115-77beb948e79d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 272
        }
      },
      "source": [
        "import gpt_2_simple as gpt2\n",
        "gpt2.download_gpt2(model_name='124M')"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:\n",
            "The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
            "For more information, please see:\n",
            "  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
            "  * https://github.com/tensorflow/addons\n",
            "  * https://github.com/tensorflow/io (for I/O related ops)\n",
            "If you depend on functionality not listed there, please file an issue.\n",
            "\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "Fetching checkpoint: 1.05Mit [00:00, 226Mit/s]                                                      \n",
            "Fetching encoder.json: 1.05Mit [00:00, 75.5Mit/s]                                                   \n",
            "Fetching hparams.json: 1.05Mit [00:00, 326Mit/s]                                                    \n",
            "Fetching model.ckpt.data-00000-of-00001: 498Mit [00:04, 112Mit/s]                                   \n",
            "Fetching model.ckpt.index: 1.05Mit [00:00, 238Mit/s]                                                \n",
            "Fetching model.ckpt.meta: 1.05Mit [00:00, 87.3Mit/s]                                                \n",
            "Fetching vocab.bpe: 1.05Mit [00:00, 133Mit/s]                                                       \n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rP2YFPgr9FIm",
        "outputId": "397329ef-fe14-4929-9781-5cc671fe6bae",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "gpt2.mount_gdrive()"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PXMpvWig9FIp"
      },
      "source": [
        "file_name = 'pride_and_prejudice.txt'\n",
        "gpt2.copy_file_from_gdrive(file_name)"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4efkiJU-9FIr",
        "outputId": "30279c6d-bca7-4259-d638-f543faa7c969",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "sess = gpt2.start_tf_sess()\n",
        "\n",
        "gpt2.finetune(\n",
        "    sess,\n",
        "    dataset=file_name,\n",
        "    model_name='124M',\n",
        "    steps=1000,\n",
        "    restore_from='fresh',\n",
        "    run_name='run1',\n",
        "    print_every=10,\n",
        "    sample_every=200,\n",
        "    save_every=500\n",
        ")"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/gpt_2_simple/src/sample.py:17: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
            "Loading checkpoint models/124M/model.ckpt\n",
            "INFO:tensorflow:Restoring parameters from models/124M/model.ckpt\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\r  0%|          | 0/1 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Loading dataset...\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "100%|██████████| 1/1 [00:01<00:00,  1.18s/it]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "dataset has 233437 tokens\n",
            "Training...\n",
            "[10 | 28.97] loss=2.57 avg=2.57\n",
            "[20 | 50.61] loss=2.37 avg=2.47\n",
            "[30 | 72.47] loss=2.41 avg=2.45\n",
            "[40 | 94.51] loss=2.20 avg=2.39\n",
            "[50 | 116.69] loss=2.26 avg=2.36\n",
            "[60 | 139.03] loss=2.23 avg=2.34\n",
            "[70 | 161.52] loss=2.24 avg=2.32\n",
            "[80 | 184.13] loss=2.13 avg=2.30\n",
            "[90 | 206.79] loss=2.10 avg=2.28\n",
            "[100 | 229.48] loss=2.21 avg=2.27\n",
            "[110 | 252.23] loss=2.06 avg=2.25\n",
            "[120 | 274.98] loss=2.18 avg=2.24\n",
            "[130 | 297.80] loss=1.94 avg=2.22\n",
            "[140 | 320.65] loss=1.92 avg=2.20\n",
            "[150 | 343.53] loss=1.84 avg=2.17\n",
            "[160 | 366.41] loss=1.77 avg=2.14\n",
            "[170 | 389.29] loss=1.80 avg=2.12\n",
            "[180 | 412.17] loss=2.00 avg=2.11\n",
            "[190 | 435.09] loss=1.74 avg=2.09\n",
            "[200 | 458.06] loss=1.66 avg=2.07\n",
            "======== SAMPLE 1 ========\n",
            " I am sorry for him,\n",
            "      he owes you an apology. I have been in love with him from\n",
            "      the very beginning. I could not keep my spirits up.”\n",
            "\n",
            "      After this long argument the quarrel was settled, and the young Mrs. Bennet\n",
            "      was married three months after.\n",
            "\n",
            "      “Miss Bennet,” said she, “your affectionate cousin,\n",
            "      I do not think it right that I should be engaged to a person less\n",
            "      capable of love, if he were really mine. I will not accept him\n",
            "      merely because I am in love with him, and I am not ready to overlook\n",
            "      his worth. I am in earnest now to see that his accomplishments in the\n",
            "      militia, as well as the commission, prove all the credit\n",
            "      you have gained by my friendship over him. My views could not be\n",
            "      changed, and when the subject of a duel is mentioned, it\n",
            "      has always been my object to prove, not by words, but by deeds\n",
            "      of which I could not offer _you_. But I will not give up the\n",
            "      cause to any doubt of his being worthy of the name, or his\n",
            "      worth. You know it, madam. It is as I see it. I knew it before I wrote\n",
            "      you, and I will know it again. I will be as strong as ever,\n",
            "      and die every one other than I have been, shall I?”\n",
            "\n",
            "      After these various proposals, Sir William’s letter was accepted and\n",
            "      received with alacrity.\n",
            "\n",
            "      Elizabeth was a most gracious, generous young lady. Her heart\n",
            "      could not for a moment be turned towards aught that pertained\n",
            "      to Sir William. Elizabeth was married for over two years. Her\n",
            "      affection to Sir William was so generous, so large, so\n",
            "      strong, that it took their father and mother many months to\n",
            "      get back. Elizabeth’s situation did not seem favourable. Her\n",
            "      wealth, her beauty, all of one kind, must be too great a\n",
            "      disadvantage to be worth living on, and her mother’s poverty\n",
            "      unable to care for her.\n",
            "\n",
            "      Lady Catherine could not help speaking so warmly of herself to\n",
            "      all these years. She was well aware that the most\n",
            "      amiable of daughters was to be neglected, and that\n",
            "      she wished to have more. Her feelings were always far\n",
            "      different; and though still in youth she still had some\n",
            "      reserve in her judgement of him, and a conviction of\n",
            "      goodness which, though often misrepresented, was almost\n",
            "      firmly preserved.\n",
            "\n",
            "      From this circumstance of her daughter’s circumstances her\n",
            "      sentiments were better explained than they were from any motive\n",
            "      of selfish curiosity. She was very well aware that though the\n",
            "      advantages which she had received from her father must have\n",
            "      tempted her to be a very high and generous girl,\n",
            "      in her own way of life it was natural. She had gained so much by\n",
            "      having a son, and so far her opinion of herself had been the highest\n",
            "      it was possible for her; had she had known his heart; and though\n",
            "      she had known Sir William’s not believing it in herself,\n",
            "      she hoped his happiness in the short space of his life to have\n",
            "      been a more than happy one.\n",
            "\n",
            "      The little effect of the time so far abated was that of a\n",
            "      very favourable one.\n",
            "\n",
            "      Elizabeth and her family were brought back to her aunt’s\n",
            "      abode a week or two sooner than she expected, and she was\n",
            "      perfectly satisfied with her family at once. But it happened,\n",
            "      she was sure, that, as the winter drew to a close, they should be\n",
            "      gone into\n",
            "\n",
            "[210 | 493.20] loss=1.53 avg=2.04\n",
            "[220 | 516.16] loss=1.62 avg=2.02\n",
            "[230 | 539.20] loss=1.86 avg=2.01\n",
            "[240 | 562.22] loss=1.68 avg=2.00\n",
            "[250 | 585.26] loss=1.66 avg=1.98\n",
            "[260 | 608.32] loss=1.31 avg=1.95\n",
            "[270 | 631.40] loss=1.69 avg=1.94\n",
            "[280 | 654.43] loss=1.54 avg=1.92\n",
            "[290 | 677.43] loss=1.40 avg=1.90\n",
            "[300 | 700.54] loss=1.31 avg=1.88\n",
            "[310 | 723.58] loss=1.21 avg=1.86\n",
            "[320 | 746.59] loss=1.28 avg=1.83\n",
            "[330 | 769.65] loss=1.37 avg=1.82\n",
            "[340 | 792.73] loss=1.30 avg=1.80\n",
            "[350 | 815.81] loss=1.26 avg=1.78\n",
            "[360 | 838.82] loss=1.26 avg=1.77\n",
            "[370 | 861.88] loss=1.15 avg=1.75\n",
            "[380 | 884.99] loss=0.89 avg=1.72\n",
            "[390 | 908.12] loss=1.01 avg=1.70\n",
            "[400 | 931.24] loss=0.89 avg=1.67\n",
            "======== SAMPLE 1 ========\n",
            ". The weather, with the\n",
            "      wetness and the roughness of the place, made the wetness of the\n",
            "      place extremely inconvenient; and, consequently, to work such\n",
            "      wet earth as might carry the horses here from Meryton,\n",
            "      in one hour’s time, and relieve it from all the inconvenience of\n",
            "      sitting down again in company with Bingley. This was a great\n",
            "      amusement to Mr. Gardiner; and after his wife had been employed\n",
            "      so hard in the office, she was in a more delightful sort of person—\n",
            "      though, as everybody likes to say, half the fun is in the other\n",
            "      men’s services. “Louisa!” said he to Elizabeth, “I thought you and Lydia\n",
            "      had a very good day. Let us be carried on, as though we had\n",
            "      every opportunity of it. Good gracious!”\n",
            "\n",
            "      “Who gives a fuss about a lady’s room? It is all theirs. If it\n",
            "      cannot be yours, it is not of my own.” And Elizabeth, who, really\n",
            "      used to love to play at the housekeeper’s party, looked as\n",
            "      anxious as usual on her friend; and without thinking to say a\n",
            "      trifle, touched him so affectionately, that he almost asked\n",
            "      to have the hearse.\n",
            "\n",
            "      “Louisa!” said he, as he started towards the dance. “You will\n",
            "      do very well, I am sure.”\n",
            "\n",
            "      “And Lydia plays! and Mr. Collins plays! but I doubt not\n",
            "      that you will have the greatest part—if your mother did not play\n",
            "      the part.”\n",
            "\n",
            "      Elizabeth made no answer, and walked on with the party; but when\n",
            "      they parted, he asked her what she was doing in Rosings;\n",
            "      and she smiled, and added:\n",
            "\n",
            "      “How I went to Brighton!”\n",
            "\n",
            "      “I did what any sensible girl would do—go there. I\n",
            "      came to Brighton. Mrs. Collins thought me very good. She\n",
            "      took me to her for her attendance there: she then made me\n",
            "      tea, and asked me to take present Mrs. Carter’s. I\n",
            "      accepted it, with great politeness, because as being a very\n",
            "      lucky success, I am to go home this summer with her, and\n",
            "      as many ladies as I please to come to Brighton. I am\n",
            "      a sensible girl, she replies,” and then adds:\n",
            "\n",
            "      “I did not, Mrs. Collins, ever think you would. You knew the\n",
            "      difference between appearances and your ideas. I now comprehend\n",
            "      what you have just told me.”\n",
            "\n",
            "      “I was surprised to see Miss Bennet at Brighton. I did not expect\n",
            "      to meet her as soon as I planned. Well, she is going to\n",
            "      be very very much in Meryton, and I shall be going\n",
            "      there myself.”\n",
            "\n",
            "      “Oh well, I hope so; and this is not the first time we have\n",
            "      been to her. She is the sister of Lady Catherine de Bourgh, her\n",
            "      nephew, who went there with her from London, and they are all good\n",
            "      friends. And you know my feelings about being there, because I\n",
            "      came here yesterday, and told the whole story to Mrs. Jenkinson. And\n",
            "      she listened to me as much as I do on the subject: ‘But,\n",
            "      Mrs. Jenkinson says we never talked among ourselves, and I dare\n",
            "      say has no fear of her getting any form of love. It is\n",
            "      such a delightful story, you might think; and in spite of all\n",
            "      my usual objections to the marrying of any man, you could\n",
            "\n",
            "\n",
            "[410 | 965.29] loss=1.22 avg=1.66\n",
            "[420 | 988.34] loss=0.91 avg=1.64\n",
            "[430 | 1011.36] loss=0.69 avg=1.61\n",
            "[440 | 1034.39] loss=1.19 avg=1.60\n",
            "[450 | 1057.42] loss=0.59 avg=1.57\n",
            "[460 | 1080.46] loss=0.77 avg=1.55\n",
            "[470 | 1103.47] loss=0.87 avg=1.53\n",
            "[480 | 1126.50] loss=0.56 avg=1.51\n",
            "[490 | 1149.51] loss=0.49 avg=1.48\n",
            "[500 | 1172.54] loss=0.78 avg=1.46\n",
            "Saving checkpoint/run1/model-500\n",
            "[510 | 1198.24] loss=0.43 avg=1.44\n",
            "[520 | 1221.20] loss=0.48 avg=1.41\n",
            "[530 | 1244.17] loss=0.25 avg=1.38\n",
            "[540 | 1267.16] loss=0.55 avg=1.36\n",
            "[550 | 1290.16] loss=0.44 avg=1.34\n",
            "[560 | 1313.18] loss=0.28 avg=1.32\n",
            "[570 | 1336.20] loss=0.30 avg=1.29\n",
            "[580 | 1359.25] loss=0.42 avg=1.27\n",
            "[590 | 1382.28] loss=0.36 avg=1.25\n",
            "[600 | 1405.32] loss=0.31 avg=1.23\n",
            "======== SAMPLE 1 ========\n",
            "Oh, I am _very, very glad I _did_. We had a very agreeable talk. My father\n",
            "      is much better, and we dined with her again. My father would not have\n",
            "      been proud to be his own master at Pemberley if he had not been so.”\n",
            "\n",
            "      “I should never have gone so—but it was such a spur to our curiosity! And so\n",
            "      many of my acquaintances were discovered such delightful people! So much\n",
            "      had been forgotten! Oh! have Lizzy and Lydia their share!”\n",
            "\n",
            "      “And what has been done, what has been attempted, what has\n",
            "      taken such a turn?”\n",
            "\n",
            "      “We have done something very remarkable.”\n",
            "\n",
            "      “And it has surprised us all.”\n",
            "\n",
            "      “And so, we should be a little silly to think how very likely it\n",
            "       will be that some day we shall all forget what we were so dreading. But,\n",
            "      perhaps, for we haven_n_ forgotten all! and therefore we\n",
            "      ought to think on the feelings of all. Had _we_ been _so! We might as\n",
            "      well have foretold the rashness of our sister’s conduct.”\n",
            "\n",
            "      “I am indeed! That thought will offend no one, and we can be silent all\n",
            "      our life. My father, silly as his opinion of us all must be, cannot\n",
            "      recall _her_ stupidity, and say, ‘She seems to defy all understanding.’\n",
            "\n",
            "      “You are quite right. _She_ has been disputable. A rational understanding\n",
            "      of all rational things, she says, _must make them fair. Such as arise from a mistaken\n",
            "      beginning, consists therefore in an error of opinion, and is therefore amiss,\n",
            "      in the\n",
            "      distribution of good and evil, which I have now reason to make. My dear madam,\n",
            "      do not see how rational concerns can be injurious to one’s\n",
            "      self, but bad. Good woman is necessary for one’s happiness.”\n",
            "\n",
            "      “Upon my word,” said Mr. Wickham, “you give way to every sense of\n",
            "      that word. I do apologise to you for having made her a fool by\n",
            "      saying _that_ girl.”\n",
            "\n",
            "      Mr. Wickham’s reply was perfectly sincere; nor were the circumstances to\n",
            "      make him desirous of changing the subject. Jane said anything to\n",
            "      lessen his distress; and Elizabeth though not immediately serious,\n",
            "      earnest enough to believe what she heard, continued most perseveringly\n",
            "      Shall we? Gentlemen, we shall all think very hard for nothing;\n",
            "      and once we think, we shall make no mistake. Her pride, she\n",
            "      earnestly insists on blaming. To me it seems inevitable. She\n",
            "      was not weak-spirited when she said no more at Lambton. She is not\n",
            "      vain! I say there may be error.”\n",
            "\n",
            "      But when success was assured them both, they declared their\n",
            "      support of the ladies, and rejoiced for their own condition. Mr.\n",
            "      Collins then took the opportunity of saying, “I do not know if I\n",
            "      ever felt more compassion to Maria (her) poor heart, but I believe her\n",
            "      feelings are not particularly amiable for defect. She is only so\n",
            "      selfish, so unprincipled, that neither her father, nor her\n",
            "      uncle, would ever allow her to be so; and I am glad to find,\n",
            "      you may depend upon it, that you have very sufficient society for\n",
            "      your sort of people. My father, however, will certainly spare\n",
            "      Maria. My father is a most agreeable-humoured man, and I hope\n",
            "      he will think the same of you. You are luckily aware, my dear\n",
            "     \n",
            "\n",
            "[610 | 1438.75] loss=0.23 avg=1.21\n",
            "[620 | 1461.76] loss=0.23 avg=1.19\n",
            "[630 | 1484.78] loss=0.28 avg=1.17\n",
            "[640 | 1507.83] loss=0.23 avg=1.15\n",
            "[650 | 1530.87] loss=0.14 avg=1.13\n",
            "[660 | 1553.92] loss=0.18 avg=1.11\n",
            "[670 | 1576.97] loss=0.15 avg=1.09\n",
            "[680 | 1600.00] loss=0.26 avg=1.07\n",
            "[690 | 1623.02] loss=0.17 avg=1.06\n",
            "[700 | 1646.05] loss=0.18 avg=1.04\n",
            "[710 | 1669.10] loss=0.17 avg=1.02\n",
            "[720 | 1692.17] loss=0.18 avg=1.01\n",
            "[730 | 1715.26] loss=0.18 avg=0.99\n",
            "[740 | 1738.36] loss=0.19 avg=0.97\n",
            "[750 | 1761.46] loss=0.17 avg=0.96\n",
            "[760 | 1784.55] loss=0.13 avg=0.94\n",
            "[770 | 1807.68] loss=0.17 avg=0.93\n",
            "[780 | 1830.82] loss=0.15 avg=0.91\n",
            "[790 | 1853.95] loss=0.11 avg=0.90\n",
            "[800 | 1877.03] loss=0.14 avg=0.89\n",
            "======== SAMPLE 1 ========\n",
            "       Elizabeth could not help laughing at so gallantry as Wickham; he\n",
            "      would have laughed at anything but the suggestion, but he had her\n",
            "      willing. He listened to her with the determined air of a man\n",
            "      determined to be heard, and then, if she chose to listen,\n",
            "      would walk away. She only smiled, and continued her speech.\n",
            "\n",
            "      “Pray, dear, what is the difference in _that_ voice from those\n",
            "      ones composedly serviceable and eloquent? Those that echo the sentiments\n",
            "      command.”\n",
            "\n",
            "      She spoke with a rapidity of speech so remarkably new, it was\n",
            "      impossible to record without reference to time and place; and the\n",
            "      same was said of her speech which, in acknowledging her brother’s\n",
            "      respect and esteem, had undergone so many trials of translation,\n",
            "      as she had yet compiled.\n",
            "\n",
            "      During his visit to Longbourn, Mr. Gardiner had the advantage of\n",
            "      hearing of two young ladies who, when they came to dinner, were\n",
            "      as well informed as either of them were of the country. Both\n",
            "      were exceedingly fond of porridge; and Gardiner, in comparing\n",
            "      it with the table manners in our neighbourhood, found\n",
            "      them very little alike. Mr. Gardiner even mentioning them as\n",
            "      certain to be of low repute had Mr. Bennet surprised her.\n",
            "\n",
            "      Mrs. Bennet saw all the disgrace which the visit would do to the\n",
            "      health of her family, and was deeply afflicted with a want of\n",
            "      decent people to represent and pity. Elizabeth, to whom Mrs.\n",
            "      Bennet was particularly anxious, was neither withstood by the\n",
            "      power of speaking, nor with the inclination of speaking with\n",
            "      skill when she saw Mr. Darcy with his usual cordiality\n",
            "      and arch elegance. She spoke highly of him, but could hardly\n",
            "      keep quiet for the parting smile of his civility.\n",
            "\n",
            "      The evening was spent chiefly in walking and talking, and\n",
            "      Elizabeth had the pleasure of welcoming Mr. Darcy, and of\n",
            "      listening to his informed and pleasant manner of expressing\n",
            "      his sorrow for what he had done. He explained the case after the\n",
            "      example of his friend Lydia, and pointed out the points of\n",
            "      perplexity and disappointment in it.\n",
            "\n",
            "      “It will not do for _us_,” said he, “to be so meanly observed. It\n",
            "      must\n",
            "      be ended with.”\n",
            "\n",
            "      “But it is _us_ favouring that we should all want to avoid him,\n",
            "      and not have him look on us with disdain,” replied Elizabeth\n",
            "      with some raptures here and there.\n",
            "\n",
            "      When they had gone through all the principal hotels in\n",
            "      Derbyshire, there was one guest, and it was a gentleman\n",
            "      of fortune, who had good humour and amiable air.\n",
            "\n",
            "      He had a large phaeton, very comfortable in the summer,\n",
            "      and, as he had seen the Westerlands, he thought he might as well\n",
            "      park his horse in the right lane, and ride away to the left.\n",
            "      On finding out that his cousin was coming towards the\n",
            "      morning, he rode about in the other phaeton, hoping,\n",
            "      however, to be noticed by the Dufferin market-place, so as to\n",
            "      avoid the two gentlemen before him. There was nothing in the\n",
            "      sight, but a fine, handsome young woman, with a fine striking\n",
            "      comb over her face, and a fine, flowing gown of straw.\n",
            "\n",
            "      She was standing a little behind Miss de Bourgh, in the\n",
            "      morning refreshment, when she was met by Mr. Darcy, and shortly\n",
            "      afterwards she entered the room\n",
            "\n",
            "[810 | 1911.13] loss=0.11 avg=0.87\n",
            "[820 | 1934.18] loss=0.14 avg=0.86\n",
            "[830 | 1957.23] loss=0.10 avg=0.85\n",
            "[840 | 1980.29] loss=0.12 avg=0.83\n",
            "[850 | 2003.40] loss=0.16 avg=0.82\n",
            "[860 | 2026.50] loss=0.09 avg=0.81\n",
            "[870 | 2049.62] loss=0.10 avg=0.80\n",
            "[880 | 2072.75] loss=0.08 avg=0.78\n",
            "[890 | 2095.86] loss=0.11 avg=0.77\n",
            "[900 | 2118.99] loss=0.10 avg=0.76\n",
            "[910 | 2142.06] loss=0.10 avg=0.75\n",
            "[920 | 2165.15] loss=0.08 avg=0.74\n",
            "[930 | 2188.24] loss=0.12 avg=0.73\n",
            "[940 | 2211.33] loss=0.08 avg=0.72\n",
            "[950 | 2234.45] loss=0.09 avg=0.71\n",
            "[960 | 2257.87] loss=0.08 avg=0.70\n",
            "[970 | 2281.26] loss=0.08 avg=0.69\n",
            "[980 | 2304.38] loss=0.08 avg=0.68\n",
            "[990 | 2327.52] loss=0.09 avg=0.67\n",
            "[1000 | 2350.73] loss=0.09 avg=0.66\n",
            "Saving checkpoint/run1/model-1000\n",
            "WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/training/saver.py:963: remove_checkpoint (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Use standard file APIs to delete files with this prefix.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5VmbQuyN9FIu"
      },
      "source": [
        "      she will now make her opinions known to the whole of the family, and\n",
        "      to all their friends.\n",
        "\n",
        "      “At a time when so many middle-aged people are moving into\n",
        "      town, when many couples are making fortunes off each other, when\n",
        "      many professions of taste are forming in the society, I am\n",
        "      really anxious to find some time here in the course of the next three\n",
        "      months to write to my dear Elizabeth, to seek her informed opinion\n",
        "      on this happy event, and to recommend it to her husband’s conduct as well\n",
        "      as her own. I often tell people to be cautious when they see\n",
        "      connections of importance here. What is to be done after my death?\n",
        "      To go through the world in such a way as to be forgotten?”\n",
        "\n",
        "      Mr. Bennet replied that he would write again at length to write\n",
        "      very near to the last lines of his letter. Elizabeth cried\n",
        "      out in alarm, and while she remained, a sense of shame had\n",
        "      entered her of her being the less attentive companion she had been\n",
        "      when she first distinguished her acquaintance. Anxiety increased\n",
        "      every moment for the other to return to her senses, and\n",
        "      every opportunity for Mr. Bennet to shine any brighter was given\n",
        "      by the very first letter.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qdOcGVo69FIu"
      },
      "source": [
        "# save model checkpoints to google drive:\n",
        "gpt2.copy_checkpoint_to_gdrive(run_name='run1')"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YEgLZ6CrH7Xp"
      },
      "source": [
        "You might have to restart colab when you reload/retrain the model (or find another workaround). "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IcyIYD8fIgoi"
      },
      "source": [
        "# colab restart suggested at this point\n",
        "%tensorflow_version 1.x\n",
        "!pip install -q gpt-2-simple\n",
        "import gpt_2_simple as gpt2\n",
        "file_name = 'pride_and_prejudice.txt'\n",
        "# if you restart sometimes not necessary:\n",
        "# gpt2.mount_gdrive()"
      ],
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FH1sXX6n9FIx",
        "outputId": "e33a990e-58af-4e71-c493-1200b11bd549",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "# 1. copy checkpoint from google drive:\n",
        "gpt2.copy_checkpoint_from_gdrive(run_name='run1')\n",
        "\n",
        "# 2. continue training:\n",
        "sess = gpt2.start_tf_sess()\n",
        "\n",
        "gpt2.finetune(\n",
        "    sess,\n",
        "    dataset=file_name,\n",
        "    model_name='124M',\n",
        "    steps=500,\n",
        "    restore_from='latest',\n",
        "    run_name='run1',\n",
        "    print_every=10,\n",
        "    overwrite=True,\n",
        "    sample_every=200,\n",
        "    save_every=100\n",
        ")\n",
        "# copy checkpoints to google drive again:\n",
        "gpt2.copy_checkpoint_to_gdrive(run_name='run1')"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/gpt_2_simple/src/sample.py:17: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
            "Loading checkpoint checkpoint/run1/model-1000\n",
            "INFO:tensorflow:Restoring parameters from checkpoint/run1/model-1000\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\r  0%|          | 0/1 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Loading dataset...\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "100%|██████████| 1/1 [00:01<00:00,  1.21s/it]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "dataset has 233437 tokens\n",
            "Training...\n",
            "Saving checkpoint/run1/model-1000\n",
            "Saving checkpoint/run1/model-1000\n",
            "======== SAMPLE 1 ========\n",
            "ours.\n",
            "\n",
            "      “_You_\n",
            "      cannot be so joking as to believe that my cousin,\n",
            "      who was always pleasant and easy, was, in fact,\n",
            "      affable of spite and cleverness. _He_ was\n",
            "      a very disagreeable sort of boy, to whom nothing could be\n",
            "      much more agreeable than a little attention. _He_ also\n",
            "      liked to be fucked.”\n",
            "\n",
            "      “Not so much by the man himself as by the _lady_’s daughter,”\n",
            "      replied Jane; “though a very good opinion can only show that\n",
            "      the male conceits out of proportion to what is known.”\n",
            "\n",
            "      “Elizabeth,” said her aunt, “gets upon everything that a\n",
            "      daughter’s mind might not want to disclose. When a father\n",
            "      deceives—and I am speaking from my own investigation, which\n",
            "      does not vary in consequence from case to case, as many people\n",
            "      who know me, I never speak of making a mistake oftener than I\n",
            "      love to be right. I love mistakes as I love other people’s. I\n",
            "      am perfectly needless, however, to acknowledge them as true. The\n",
            "      apothecary who examined your sister said,”\n",
            "\n",
            "      “And you saw her making some alterations in the lace, and\n",
            "      yourself—oh! it was so small! and you thought it worth\n",
            "      saying!—but no; it was not worth saying. She hardly ever\n",
            "      changed her ways. She was never caught saying what was\n",
            "      much better. It was overheard between Miss de Bourgh and\n",
            "      the chief of the manor:—\n",
            "\n",
            "      “He asked her whether she was sure you could be certain that\n",
            "      the\n",
            "     daughter of King George V had been brought up at all? If she\n",
            "      answered no, to be sure it could not be farther from her\n",
            "      words, than impossible that she might have been the last person\n",
            "      forgotten. If she—if only knew what had happened to Mr.\n",
            "      and Miss de Bourgh—had not been deliberately misleading\n",
            "      us in her communication of the chief of the ——shire, in an\n",
            "      attempt to discredit us, by glamorising her as his\n",
            "      sister she might actually have made it known to the King.”\n",
            "\n",
            "      “But perhaps it was only a matter of time. At any rate, Mr.\n",
            "      Bingley,” observed her, “I cannot quite understand it. If\n",
            "      the conceited life which now makes a shameless scheme, of which\n",
            "      you have so much or any intention, habitual or irregular, so\n",
            "      young, so unexpected—” run over and grab a few rocks from the\n",
            "      bough-cloth, and wean it well. I have got Mr. Bingley afraid.\n",
            "      He is not of a colour, I know, if he were ever brought up under\n",
            "      my protection. I always had a good opinion of him; but in\n",
            "      recent memory, I have heard only that he was very\n",
            "      opposed to the sight of her.”\n",
            "\n",
            "      “I was afraid you might have very unfortunate regrets.”\n",
            "\n",
            "      “Yes,” said Jane, “I am afraid—and it is of no consequence\n",
            "      now. But if _we_ do not look we may as well wait.”\n",
            "\n",
            "      “Can I have the letter meanwhile delivered?”\n",
            "\n",
            "      “No, I’ll have it while I can.”\n",
            "\n",
            "      With this say:\n",
            "\n",
            "      “Your cousin has just called me into his house to hear the\n",
            "      news; and he has something very much for which he is not\n",
            "      familiarly conversant. I have never liked him, I know\n",
            "\n",
            "[1010 | 44.34] loss=0.17 avg=0.17\n",
            "[1020 | 66.08] loss=0.11 avg=0.14\n",
            "[1030 | 88.04] loss=0.09 avg=0.12\n",
            "[1040 | 110.17] loss=0.09 avg=0.11\n",
            "[1050 | 132.50] loss=0.10 avg=0.11\n",
            "[1060 | 154.99] loss=0.07 avg=0.10\n",
            "[1070 | 177.50] loss=0.08 avg=0.10\n",
            "[1080 | 200.14] loss=0.07 avg=0.10\n",
            "[1090 | 222.87] loss=0.09 avg=0.10\n",
            "[1100 | 245.70] loss=0.10 avg=0.10\n",
            "Saving checkpoint/run1/model-1100\n",
            "WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/training/saver.py:963: remove_checkpoint (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Use standard file APIs to delete files with this prefix.\n",
            "[1110 | 271.00] loss=0.10 avg=0.10\n",
            "[1120 | 293.84] loss=0.08 avg=0.09\n",
            "[1130 | 316.74] loss=0.06 avg=0.09\n",
            "[1140 | 339.68] loss=0.08 avg=0.09\n",
            "[1150 | 362.67] loss=0.06 avg=0.09\n",
            "[1160 | 385.67] loss=0.07 avg=0.09\n",
            "[1170 | 408.70] loss=0.11 avg=0.09\n",
            "[1180 | 431.75] loss=0.11 avg=0.09\n",
            "[1190 | 454.81] loss=0.08 avg=0.09\n",
            "[1200 | 477.91] loss=0.08 avg=0.09\n",
            "Saving checkpoint/run1/model-1200\n",
            "======== SAMPLE 1 ========\n",
            " where the whole has got\n",
            "      changed hands.\n",
            "\n",
            "      From Longbourn to Rosings, from that part of the letter\n",
            "      to Elizabeth, they reached Lydia. Elizabeth was frightened. She had never\n",
            "      heard Elizabeth so speak ill of Elizabeth before; and every memory\n",
            "      of Lydia’s derision made her more anxious than usual to find\n",
            "      it out. But it was impossible. No, I have not the honour to ask\n",
            "      yourself this; but how often have you had the misfortune to\n",
            "      learn that your sister is not quite so small as she thinks she can\n",
            "      manage! How well it sounds! But I can assure you that my sister,\n",
            "      when she is educated, shall certainly boast less of her\n",
            "      unwillingness than I canf she chooses.”\n",
            "\n",
            "      As it happened that Elizabeth most doubted her own affection,\n",
            "      her whole heart softened; and even her most tenderest attachment to Jane\n",
            "      seemed fair, and all her attachment to Bingley was impelled by\n",
            "      the hope of an age when she would be looked up to as a\n",
            "      special class preference.\n",
            "\n",
            "      “I would no stretch the possibility of my own being any cause of\n",
            "      my elopement,” said she, “where I am only very young. No\n",
            "      man in his or her early thirties can have been so much in love with\n",
            "      me as I thought myself. Of this love, however, I may fairly\n",
            "      hope to date the least portion.”\n",
            "\n",
            "      “But last summer I gave way,” said Wickham, “as a turning-point,”\n",
            "      and a turning-point of my youth. I had myself in fact the\n",
            "      disfavour of refusing service; and since then, however, I\n",
            "      have changed all that. I write, I talk, I speak, not of mere\n",
            "      vanity or pride, but of the unblemished, the enlightened mind\n",
            "      which facts, as they now occur, often overlook or\n",
            "      deny. Let me foremost your opinion. Why did you not\n",
            "      tell your sister that I had left her no money, that I was\n",
            "      thus deprived of every living thing she had left? And why did\n",
            "      you not tell her that I had deserted his cousin?”\n",
            "\n",
            "      “I did not leave her no living thing for ever. I leave her no\n",
            "      doubt as to the motive.”\n",
            "\n",
            "      “Shall I ask you that point,” added your partner? “Shall\n",
            "      ask your cousin any questions?”\n",
            "\n",
            "      “The one at which I speak, indeed; for I must leave you\n",
            "      with permission for the other.”\n",
            "\n",
            "      Mr. Bingley was so diffident as to forbore entering into\n",
            "      conversation with either of your sisters. They were obliged to\n",
            "      dance a little, and avoid each other every way they knew. Mrs.\n",
            "      Bennet was obliged to keep her distance from Lucas, and\n",
            "      oppose her husband’s. Elizabeth was muchmed into taking part,\n",
            "      and was carefully prepared for the interview. She was not\n",
            "      better acquainted with him than he was, but he was\n",
            "      quite an stranger to her. She saw him only in the hope of\n",
            "      talking to him again, and almost forgot what he said before\n",
            "      her uncle and aunt. She found that he was as good as ever talked to\n",
            "      them, and that every sentence was in a heightened light; and the\n",
            "      recollections which were at the beginning of good and bad each\n",
            "      ought to exasperate, were revealed into their uncle and\n",
            "      aunt.\n",
            "\n",
            "      “It is unlucky,” said their uncle, “that you should not have the\n",
            "      advantage of your chance, or that you should be obliged to\n",
            " \n",
            "\n",
            "[1210 | 514.29] loss=0.08 avg=0.09\n",
            "[1220 | 537.30] loss=0.08 avg=0.09\n",
            "[1230 | 560.34] loss=0.08 avg=0.09\n",
            "[1240 | 583.42] loss=0.07 avg=0.09\n",
            "[1250 | 606.50] loss=0.07 avg=0.09\n",
            "[1260 | 629.57] loss=0.08 avg=0.09\n",
            "[1270 | 652.66] loss=0.06 avg=0.08\n",
            "[1280 | 675.76] loss=0.07 avg=0.08\n",
            "[1290 | 698.86] loss=0.09 avg=0.08\n",
            "[1300 | 721.91] loss=0.06 avg=0.08\n",
            "Saving checkpoint/run1/model-1300\n",
            "[1310 | 747.27] loss=0.06 avg=0.08\n",
            "[1320 | 770.28] loss=0.06 avg=0.08\n",
            "[1330 | 793.32] loss=0.06 avg=0.08\n",
            "[1340 | 816.37] loss=0.06 avg=0.08\n",
            "[1350 | 839.43] loss=0.07 avg=0.08\n",
            "[1360 | 862.51] loss=0.08 avg=0.08\n",
            "[1370 | 885.58] loss=0.05 avg=0.08\n",
            "[1380 | 908.66] loss=0.07 avg=0.08\n",
            "[1390 | 931.75] loss=0.08 avg=0.08\n",
            "[1400 | 954.84] loss=0.06 avg=0.08\n",
            "Saving checkpoint/run1/model-1400\n",
            "======== SAMPLE 1 ========\n",
            ", but that he _must_ return to\n",
            "      Netherfield on Tuesday, as soon as he can, to dinner. I shall send\n",
            "      him again by carriage, with his servants, to dinner. You\n",
            "      must send John some letter-tables, as he is going on business\n",
            "     liquely. My eldest sister will have my brother and the\n",
            "      two young ladies by the time I come to them. The officers may\n",
            "      not be enough for my security, though my youngest sister is\n",
            "      obliged to be idle, and Mrs. Collins has enough of me. I\n",
            "      shall go undercover of lodgings, and inform the young ladies to\n",
            "      go themselves, when I am gone. There is nothing I can do, but to\n",
            "      keep going on business as usual. My brother and sister-in-law are\n",
            "      going to be in London again, and I hope they willorder you there\n",
            "      before they go away. Stay, stay, stay, stay, stay, stay, stay, stay, stay,\n",
            "      stay, stay, Cormac.\n",
            "\n",
            "      One day, as he was walking with his sister through the hall-ends,\n",
            "      her uncle was standing under the leaves of a homegrown pomegranate, and\n",
            "      he said to her,\n",
            "\n",
            "      “My eldest sister, am I to make the most of an early\n",
            "      visit?”\n",
            "\n",
            "      She answered her sister was not in a position to make an early return, and\n",
            "      after a short pause, added:\n",
            "\n",
            "      “It is very much my wish that you should be able to see them\n",
            "      by themselves, because they are family to us all.”\n",
            "\n",
            "      On this Miss Bingley’s reply, of whose deference she was in no\n",
            "      doubt, was heightened by some sensations of curiosity; and she\n",
            "      related the affair to her uncle, who was determined to-morrow\n",
            "      to dine with him, and to avoid a shameful event that had been\n",
            "      planned.\n",
            "\n",
            "      “It is fortunate,” said she, as they walked on, “that Miss\n",
            "      Bingley does not suffer from a state of extravagantness.”\n",
            "\n",
            "      This went on, and the sisters got on well together. Miss Bingley\n",
            "      was not always so pleasing to the mind.\n",
            "\n",
            "      The card-table, in which this event had taken place, had\n",
            "      a pleasing resemblance of real life. The furniture was\n",
            "      polished, and the freshet was of tolerable good — though not very\n",
            "      elegant. Miss Bingley looked just as she had been used to look\n",
            "      the morning after; but her features, such as she had not supposed\n",
            "      of her to be fitted for a fight, and her manner of walking,\n",
            "      had been fixed on by most women to be obtained from familiar\n",
            "      sources. Her figure was formed, and her manner perfectly\n",
            "      imagined. Her features, however, were formed still better, and\n",
            "      their difference pronounced. At first they became a little\n",
            "      disputable, for she frequently spoke of the use of the\n",
            "      nephew, and of all the perverseness of her situation in life. But\n",
            "      they became just as they had been used to be, and she lost\n",
            "      all pretensions to them.\n",
            "\n",
            "      The conversation soon turned upon fishing; and Elizabeth,\n",
            "      having much to say to Miss Bingley, with more calmness than\n",
            "      usual, than on the subject might be expected. Miss\n",
            "      Darcy’s attention was chiefly fixed on the other girls, who\n",
            "      were all employed in watching how little Miss Bennet was engaged\n",
            "      in talking; and Miss Bingley had nothing to say but the advice\n",
            "      of her sister and of Mr. Darcy, who attended them together.\n",
            "\n",
            "      The three courses which Elizabeth had mentioned, which she considered\n",
            " \n",
            "\n",
            "[1410 | 990.72] loss=0.06 avg=0.08\n",
            "[1420 | 1013.69] loss=0.08 avg=0.08\n",
            "[1430 | 1036.67] loss=0.07 avg=0.08\n",
            "[1440 | 1059.69] loss=0.05 avg=0.08\n",
            "[1450 | 1082.74] loss=0.06 avg=0.08\n",
            "[1460 | 1105.77] loss=0.06 avg=0.08\n",
            "[1470 | 1128.79] loss=0.06 avg=0.08\n",
            "[1480 | 1151.86] loss=0.06 avg=0.08\n",
            "[1490 | 1174.92] loss=0.06 avg=0.07\n",
            "[1500 | 1198.00] loss=0.08 avg=0.07\n",
            "Saving checkpoint/run1/model-1500\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ceD_sim49FI0"
      },
      "source": [
        "# Generating text"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8w-cucvZ9FI0",
        "outputId": "30d1b188-d576-47cf-a3e3-ecfcd0d88754",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "# colab restart suggested here\n",
        "%tensorflow_version 1.x\n",
        "!pip install -q gpt-2-simple\n",
        "import gpt_2_simple as gpt2\n",
        "file_name = 'pride_and_prejudice.txt'\n",
        "# if you restart, sometimes not necessary:\n",
        "gpt2.mount_gdrive()\n",
        "\n",
        "gpt2.copy_checkpoint_from_gdrive(run_name='run1')\n",
        "sess = gpt2.start_tf_sess()\n",
        "gpt2.load_gpt2(sess, run_name='run1')"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n",
            "Loading checkpoint checkpoint/run1/model-1500\n",
            "INFO:tensorflow:Restoring parameters from checkpoint/run1/model-1500\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZDRLT79Sh9j5"
      },
      "source": [
        "Please note that you can adjust the length of the returned text (length parameter) in the cell below."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9pks2x3P9FI3",
        "outputId": "522fb415-5e4d-4be4-ab7f-c431209c2206",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        }
      },
      "source": [
        "import datetime\n",
        "from google.colab import files\n",
        "\n",
        "gen_file = 'gpt2_gentext_{:%Y%m%d_%H%M%S}.txt'.format(\n",
        "    datetime.datetime.utcnow()\n",
        ")\n",
        "\n",
        "gpt2.generate_to_file(\n",
        "  sess,\n",
        "  destination_path=gen_file,\n",
        "  #length=500,\n",
        "  temperature=0.7,\n",
        "  nsamples=100,\n",
        "  batch_size=20\n",
        ")\n",
        "files.download(gen_file)"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "\n",
              "    async function download(id, filename, size) {\n",
              "      if (!google.colab.kernel.accessAllowed) {\n",
              "        return;\n",
              "      }\n",
              "      const div = document.createElement('div');\n",
              "      const label = document.createElement('label');\n",
              "      label.textContent = `Downloading \"${filename}\": `;\n",
              "      div.appendChild(label);\n",
              "      const progress = document.createElement('progress');\n",
              "      progress.max = size;\n",
              "      div.appendChild(progress);\n",
              "      document.body.appendChild(div);\n",
              "\n",
              "      const buffers = [];\n",
              "      let downloaded = 0;\n",
              "\n",
              "      const channel = await google.colab.kernel.comms.open(id);\n",
              "      // Send a message to notify the kernel that we're ready.\n",
              "      channel.send({})\n",
              "\n",
              "      for await (const message of channel.messages) {\n",
              "        // Send a message to notify the kernel that we're ready.\n",
              "        channel.send({})\n",
              "        if (message.buffers) {\n",
              "          for (const buffer of message.buffers) {\n",
              "            buffers.push(buffer);\n",
              "            downloaded += buffer.byteLength;\n",
              "            progress.value = downloaded;\n",
              "          }\n",
              "        }\n",
              "      }\n",
              "      const blob = new Blob(buffers, {type: 'application/binary'});\n",
              "      const a = document.createElement('a');\n",
              "      a.href = window.URL.createObjectURL(blob);\n",
              "      a.download = filename;\n",
              "      div.appendChild(a);\n",
              "      a.click();\n",
              "      div.remove();\n",
              "    }\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "download(\"download_010d1598-ece1-431e-ac4b-5be509bb1cab\", \"gpt2_gentext_20201026_152702.txt\", 331837)"
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3_TZUceBQa7A"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}